package com.baturu.vin.benz.service.impl;

import com.baturu.vin.benz.dto.BenzEPCPartsInfoDTO;
import com.baturu.vin.benz.dto.BenzSpring;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author jiangxinlei
 * @Time 2017/4/25.
 */
@Service("benzSpringService")
public class BenzSpringService {


    public List<BenzEPCPartsInfoDTO> validateSpring(List<BenzEPCPartsInfoDTO> parts, BenzSpring benzSpring) {
        if (CollectionUtils.isEmpty(parts)) {
            return parts;
        }
        try {
            List<String> springParts = fillSpringParts(benzSpring);
            if (springParts.isEmpty()) {
                return parts;
            }
            List<String> springCallouts = findSpringCallouts(parts, springParts);
            if (springCallouts.isEmpty()) {
                return parts;
            }
            parts = findReplacedAndOptionalParts(parts, springCallouts, springParts);
        } catch (Exception ignored) {

        }
        return parts;
    }


    private List<BenzEPCPartsInfoDTO> findReplacedAndOptionalParts(List<BenzEPCPartsInfoDTO> parts, List<String> springCallouts, List<String> springParts) {
        for (Object element : parts) {
            if (element instanceof BenzEPCPartsInfoDTO) {
                BenzEPCPartsInfoDTO part = (BenzEPCPartsInfoDTO) element;
                if (part.getCallout() == null || !springCallouts.contains(part.getCallout()) || part.getCbPartsCode() == null || !springParts.contains(part.getCbPartsCode())) {
                    continue;
                }
                if (part.getReplFlag() != null && part.getReptype() != null && part.getReppno() != null) {
                    String replacePart = part.getReptype().trim() + part.getReppno().trim();
                    if (!springParts.contains(replacePart)) {
                        springParts.add(replacePart);
                        parts = findReplacedAndOptionalParts(parts, springCallouts, springParts);
                    }
                }
                if (part.getOptflag() == null || part.getOptPart() == null) {
                    continue;
                }
                String optPart = part.getOptPart();
                boolean valid = false;
                for (int i = 0; i < optPart.length() - 20; i += 20) {
                    String tmpPart = optPart.substring(i, i + 20);
                    tmpPart = tmpPart.replaceAll(" ", "");
                    optPart = optPart.substring(20);
                    if (!springParts.contains(tmpPart)) {
                        springParts.add(tmpPart);
                        valid = true;
                    }
                }
                if (!optPart.isEmpty()) {
                    String tmpPart2 = optPart;
                    tmpPart2 = tmpPart2.replaceAll(" ", "");
                    if (!springParts.contains(tmpPart2)) {
                        springParts.add(tmpPart2);
                        valid = true;
                    }
                }
                if (!valid) {
                    continue;
                }
                parts = findReplacedAndOptionalParts(parts, springCallouts, springParts);
            }
        }
        return parts;
    }

    private List<String> fillSpringParts(BenzSpring springs) {
        List<String> springParts = Lists.newArrayList();
        if (springs != null && !springs.getSpringParts().trim().isEmpty()) {
            Collections.addAll(springParts, springs.getSpringParts().split(","));
        }
        return springParts;
    }

    private List<String> findSpringCallouts(List<BenzEPCPartsInfoDTO> originalParts, List<String> springParts) {
        List<String> springCallouts = Lists.newArrayList();
        for (Object element : originalParts) {
            if (element instanceof BenzEPCPartsInfoDTO) {
                BenzEPCPartsInfoDTO part = (BenzEPCPartsInfoDTO) element;
                if (part.getCbPartsCode() == null || !springParts.contains(part.getCbPartsCode()) || part.getCallout() == null || springCallouts.contains(part.getCallout())) {
                    continue;
                }
                springCallouts.add(part.getCallout());
            }
        }
        return springCallouts;
    }
}
